﻿using Microsoft.Win32;
using MyWPF.StaticData;
using Newtonsoft.Json;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Security.AccessControl;
using MyWPF.Views.Others;
using IWshRuntimeLibrary;
using System.Collections.ObjectModel;
using System.Windows;

namespace MyWPF.ViewModel.Setting
{
    public class VM_SetWindow_Normal : BaseViewModel
    {
        public VM_SetWindow_Normal()
        {
            #region 初始化用户选项
            LanguageSource = new ObservableCollection<Language>(Enum.GetValues(typeof(Language)).Cast<Language>());
            #endregion
        }




        #region 属性绑定：两个开关
        /// <summary>
        /// 开机自启动
        /// </summary>
        public bool AutoStart
        {
            get { return _autoStart; }
            set
            {
                StaticModels.staticInfo.config.NomalConfig.AutoStart = value;
                //将StaticModels.staticInfo.config序列化后保存
                string configStr = JsonConvert.SerializeObject(StaticModels.staticInfo.config);
                var filePath = "Assets/Json/Config.json";
                System.IO.File.WriteAllText(filePath, configStr);
                //保存值
                SetProperty(ref _autoStart, value);

                if (value)
                {
                    try
                    {
                        //获取windows自启动文件夹路径
                        string startupFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
                        //获取当前软件路径
                        string exePath = System.Windows.Forms.Application.ExecutablePath;
                        string exeStartPath = exePath.Replace(@"\MyWPF.exe", "");

                        WshShell wshShell = new WshShell();
                        WshShortcut wshShortcut = wshShell.CreateShortcut(startupFolderPath + @"\" + StaticModels.staticInfo.config.VersionConfig.WinCCName + ".lnk");
                        wshShortcut.TargetPath = exePath;
                        wshShortcut.WorkingDirectory = exeStartPath;
                        wshShortcut.Save();

                    }
                    catch (Exception ex)
                    {
                        Tips tips = new Tips("请用管理员权限启动本程序！");
                        tips.ShowDialog();
                        AutoStart = false;
                    }

                    //填写日志
                    TimeSpan TimeDifference = DateTime.Now - StaticModels.StartTime;
                    TimeSpan TimeStandard = new TimeSpan(2000000);
                    if (TimeDifference.TotalSeconds > 10)
                    {
                        StaticModels.journal.AddJournal(new Models.Journal.JournalContext()
                        {
                            Context = "开机自启动打开",
                            Time = DateTime.Now,
                            Type = Models.Journal.JournalType.日志,
                            User = StaticModels.user.User.UserName
                        });
                    }

                }
                else
                {
                    string LnkPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup) + @"\" + StaticModels.staticInfo.config.VersionConfig.WinCCName + ".lnk";
                    System.IO.File.Delete(LnkPath);
                    //填写日志
                    TimeSpan TimeDifference = DateTime.Now - StaticModels.StartTime;
                    TimeSpan TimeStandard = new TimeSpan(2000000);
                    if (TimeDifference.TotalSeconds > 10)
                    {
                        StaticModels.journal.AddJournal(new Models.Journal.JournalContext()
                        {
                            Context = "开机自启动关闭",
                            Time = DateTime.Now,
                            Type = Models.Journal.JournalType.日志,
                            User = StaticModels.user.User.UserName
                        });
                    }

                }

            }
        }
        private bool _autoStart;
        /// <summary>
        /// 禁止多开
        /// </summary>
        public bool BanMore
        {
            get { return _banMore; }
            set
            {
                StaticModels.staticInfo.config.NomalConfig.BanMore = value;
                //将StaticModels.staticInfo.config序列化后保存
                string configStr = JsonConvert.SerializeObject(StaticModels.staticInfo.config);
                var filePath = "Assets/Json/Config.json";
                System.IO.File.WriteAllText(filePath, configStr);
                SetProperty(ref _banMore, value);

                if (value)
                {
                    //填写日志
                    TimeSpan TimeDifference = DateTime.Now - StaticModels.StartTime;
                    TimeSpan TimeStandard = new TimeSpan(2000000);
                    if (TimeDifference.TotalSeconds > 10)
                    {
                        StaticModels.journal.AddJournal(new Models.Journal.JournalContext()
                        {
                            Context = "开机禁止多开",
                            Time = DateTime.Now,
                            Type = Models.Journal.JournalType.日志,
                            User = StaticModels.user.User.UserName
                        });
                    }
                }
                else
                {
                    //填写日志
                    TimeSpan TimeDifference = DateTime.Now - StaticModels.StartTime;
                    TimeSpan TimeStandard = new TimeSpan(2000000);
                    if (TimeDifference.TotalSeconds > 10)
                    {
                        StaticModels.journal.AddJournal(new Models.Journal.JournalContext()
                        {
                            Context = "关闭禁止多开",
                            Time = DateTime.Now,
                            Type = Models.Journal.JournalType.日志,
                            User = StaticModels.user.User.UserName
                        });
                    }
                }
            }
        }
        private bool _banMore;
        #endregion

        #region 属性绑定：用户选择的语言
        /// <summary>
        /// 所有语言选项
        /// </summary>
        public ObservableCollection<Language> LanguageSource
        {
            get { return _languageSource; }
            set { SetProperty(ref _languageSource, value); }
        }
        private ObservableCollection<Language> _languageSource;
        /// <summary>
        /// 用户选择的语言
        /// </summary>
        public Language LanguageSelected
        {
            get { return _languageSelected; }
            set
            {
                SetProperty(ref _languageSelected, value);
                switch (value)
                {
                    case Language.English:
                        // 从Application.Current.Resources中获取ResourceDictionary
                        var mergedDictionaries = Application.Current.Resources.MergedDictionaries;

                        // 查找要删除的ResourceDictionary
                        var chineseDictionary = mergedDictionaries.FirstOrDefault(d => d.Source?.OriginalString == "Assets/Language/Chinese.xaml");

                        // 如果找到了中文资源字典，则将其从MergedDictionaries中删除
                        if (chineseDictionary != null)
                        {
                            mergedDictionaries.Remove(chineseDictionary);
                        }

                        // 添加英文资源字典
                        mergedDictionaries.Add(new ResourceDictionary
                        {
                            Source = new Uri("Assets/Language/English.xaml", UriKind.Relative)
                        });

                        //保存到配置
                        StaticModels.staticInfo.config.NomalConfig.Language = value;
                        string configStr = JsonConvert.SerializeObject(StaticModels.staticInfo.config);
                        var filePath = "Assets/Json/Config.json";
                        System.IO.File.WriteAllText(filePath, configStr);
                        break;
                    default:
                        // 从Application.Current.Resources中获取ResourceDictionary
                        mergedDictionaries = Application.Current.Resources.MergedDictionaries;

                        // 查找要删除的ResourceDictionary
                        chineseDictionary = mergedDictionaries.FirstOrDefault(d => d.Source?.OriginalString == "Assets/Language/English.xaml");

                        // 如果找到了中文资源字典，则将其从MergedDictionaries中删除
                        if (chineseDictionary != null)
                        {
                            mergedDictionaries.Remove(chineseDictionary);
                        }

                        // 添加中文资源字典
                        mergedDictionaries.Add(new ResourceDictionary
                        {
                            Source = new Uri("Assets/Language/Chinese.xaml", UriKind.Relative)
                        });

                        //保存到配置
                        StaticModels.staticInfo.config.NomalConfig.Language = value;
                        configStr = JsonConvert.SerializeObject(StaticModels.staticInfo.config);
                        filePath = "Assets/Json/Config.json";
                        System.IO.File.WriteAllText(filePath, configStr);
                        break;
                }
            }
        }
        private Language _languageSelected;
        #endregion


    }
}
